﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace OnlineCooperation
{
    /// <summary>
    /// PersonalFileDownload 的摘要说明
    /// </summary>
    public class PersonalFileDownload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            if (context.Session["CurrentDir"] == null || context.Session["CurrentDir"].ToString() == "")
                return;

            String dir = context.Session["CurrentDir"].ToString();

            context.Response.ContentType = "text/plain";

            string file_name = context.Request["FileName"];

            string path = (dir + file_name);//必须在 文件夹 后边多加  / ,不然 获得的路径错误！！！

            
            FileInfo fileInfo = new FileInfo(path);
            context.Response.Clear();
            context.Response.ClearContent();
            context.Response.ClearHeaders();
            context.Response.AddHeader("Content-Disposition", "attachment;filename=" + file_name);
            context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            context.Response.AddHeader("Content-Transfer-Encoding", "binary");
            context.Response.ContentType = "application/octet-stream";
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            context.Response.WriteFile(fileInfo.FullName);
            context.Response.Flush();
            context.Response.End();


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}